-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/29
-- @module:   ui
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class ui
local ui              = {}
local this            = ui
this.spCfg            = nil

local import          = import
local trace           = trace
local ui_unit         = ui_unit
local npc_shop_unit   = npc_shop_unit
local item_unit       = item_unit
local achieve_unit    = achieve_unit
local mastery_unit    = mastery_unit
local collection_unit = collection_unit
local actor_unit      = actor_unit
local sign_unit       = sign_unit
local mail_unit       = mail_unit
local exchange_unit   = exchange_unit
local make_unit       = make_unit
local guild_unit      = guild_unit
local map_unit        = map_unit
local cash_unit       = cash_unit
local party_unit      = party_unit

---@type ui_cfg
local ui_cfg          = import("game/config/ui_cfg")
---@type api
local api             = import("api/api")
---@type role
local role            = import("game/func/role")
-- 点击窗口按钮
function ui.click_window_btn(name)
    if not name then
        return false
    end
    local ret, control = this.check_ui_info(name)
    if ret then
        if name ~= "钱包二维码关闭按钮" then
            if ui_unit.btn_is_visible(control) and not ui_unit.btn_is_disabled(control) then
                ui_unit.btn_click(control)
                return true
            end
        else
            if ui_unit.btn_is_visible(control) then
                ui_unit.click_web_close_btn(control)
                return true
            end
        end
    end
    return false
end

-- 检查ui控件是否存在
function this.check_ui_info(name)
    local c_ui = ui_cfg.UI_INFO[name]
    if c_ui and c_ui.ui_path then
        --xxmsg(c_ui.ui_path)
        local list = ui_unit.get_child_control(c_ui.ui_path, true)
        --local list2 = api.get_ui_child_control_list(c_ui.ui_path)
        --print_r(list2)
        if list ~= 0 then
            return true, list
        end
    end
    return false
end

-- 检查窗口是否显示
function this.check_window(name)
    local ret, control = this.check_ui_info(name)
    if ret then
        return true
    end
    return false
end

-- 特殊窗口的打开关闭
function this.get_sp_ui_cfg()
    if not this.spCfg then
        this.spCfg = {
            { name = '背包按钮', check = -1, openName = '背包', call = function() return item_unit.open_inventory() end },
            { name = '背包关闭按钮', check = 1, openName = '背包', call = function() return item_unit.close_inventory() end },
            { name = '装备分解打开按钮', check = -1, openName = '装备分解窗口', call = function() return item_unit.open_decompose() end },
            {
                name = '熟练度界面打开按钮',
                check = -1,
                openName = '熟练度界面',
                call = function()
                    return mastery_unit
                        .open_mastery_scene()
                end
            },
            {
                name = '熟练度学习界面打开按钮',
                check = -1,
                openName = '熟练度学习界面',
                call = function()
                    return mastery_unit
                        .open_mastery_popup()
                end
            },
            {
                name = '熟练度学习界面关闭按钮',
                check = 1,
                openName = '熟练度学习界面',
                call = function()
                    return mastery_unit.close_mastery_popup()
                end
            },
            {
                name = '成就界面打开按钮',
                check = -1,
                openName = '成就主界面',
                call = function()
                    return achieve_unit
                        .open_achieve_scene()
                end
            },
            {
                name = '收藏窗口打开按钮',
                check = -1,
                openName = '收藏窗口',
                call = function()
                    return collection_unit.open_collection_scene()
                end
            },
            { name = '属性加点窗口打开按钮', check = -1, openName = '属性加点窗口', call = function() return actor_unit.open_stat_scene() end },
            { name = '活动窗口打开按钮', check = -1, openName = '活动窗口', call = function() return sign_unit.open_event_popup() end },
            { name = '活动窗口关闭按钮', check = 1, openName = '活动窗口', call = function() return role.key_control("KEY_Esc") end },
            { name = '邮箱窗口打开按钮', check = -1, openName = '邮箱窗口', call = function() return mail_unit.open_mail_scene() end },
            { name = '交易行窗口打开', check = -1, openName = '交易行窗口', call = function() return exchange_unit.open_exchange() end },
            { name = '打开制造窗口', check = -1, openName = '制造窗口', call = function() return make_unit.open_make_scene() end },
            { name = '关闭制造窗口', check = 1, openName = '制造窗口', call = function() return ui_unit.exit_widget() end },
            { name = '打开仓库窗口', check = -1, openName = '仓库窗口', call = function(npcId) return item_unit.open_storage(npcId) end },
            { name = '打开强化窗口', check = -1, openName = '强化窗口', call = function() return item_unit.open_enhancement_scene() end },
            { name = '关闭强化窗口', check = 1, openName = '强化窗口', call = function() return ui_unit.exit_widget() end },
            { name = '关闭制造窗口', check = -1, openName = '制造窗口', call = function() return ui_unit.exit_widget() end },
            { name = '打开公会窗口', check = -1, openName = '公会窗口', call = function() return guild_unit.open_guild_widget() end },
            { name = '点击领取成功', check = -1, openName = '成功窗口', call = function() return item_unit.close_acquire_popup() end },
            {
                name = '打开制造类型选择页面',
                check = -1,
                openName = '制造类型选择页面',
                call = function()
                    return make_unit
                        .open_make_gate_scene()
                end
            },

        }
    end
    return this.spCfg
end

-- 是否打开特殊窗口 return 1/打开 -1/没打开 0/没有找到
function this.isOpenSpWindow(name)
    if not this.spOpenWindowCfg then
        this.spOpenWindowCfg = {
            { name = '背包', func = function() return item_unit.is_open_inventory() end },
            { name = '成功窗口', func = function() return item_unit.has_acquire_popup() end },
            { name = '装备分解窗口', func = function() return item_unit.is_open_decompose() end },
            { name = '熟练度界面', func = function() return mastery_unit.is_open_mastery_scene() end },
            { name = '熟练度学习界面', func = function() return mastery_unit.is_open_mastery_popup() end },
            { name = '成就主界面', func = function() return achieve_unit.is_open_achieve_scene() end },
            { name = '收藏窗口', func = function() return collection_unit.is_open_collection_scene() end },
            { name = '属性加点窗口', func = function() return actor_unit.is_open_stat_scene() end },
            { name = '活动窗口', func = function() return sign_unit.is_open_event_popup() end },
            { name = '通行证窗口', func = function() return sign_unit.season_pass_popup_is_open() end },
            { name = '邮箱窗口', func = function() return mail_unit.mail_scene_is_open() end },
            { name = '交易行窗口', func = function() return exchange_unit.exchange_is_open() end },
            { name = '制造窗口', func = function() return make_unit.is_open_make_scene() end },
            { name = '仓库窗口', func = function() return item_unit.get_ware_house_scene() ~= 0 end },
            { name = 'npc商店窗口', func = function() return npc_shop_unit.get_npc_store_scene() ~= 0 end },
            { name = '出售挂件', func = function() return npc_shop_unit.get_delivery_asset_popup() end },
            { name = '公会窗口', func = function() return guild_unit.is_open_guild_widget() end },
            { name = '强化窗口', func = function() return item_unit.get_enhancement_scene() ~= 0 end },
            { name = '交易行出售页面', func = function() return exchange_unit.get_exchange_page() == 1 end },
            { name = '交易行购买页面', func = function() return exchange_unit.get_exchange_page() == 0 end },
            { name = '转职窗口', func = function() return actor_unit.pcclass_widget_is_open() end },
            { name = '制造类型选择页面', func = function() return make_unit.is_open_make_gate_scene() end },
            { name = '制造专业选择页面', func = function() return make_unit.get_craft_skill_scene() ~= 0 end },
            { name = '专业制造窗口', func = function() return make_unit.get_item_craft_scene() ~= 0 end },
            { name = '激战地进入窗口', func = function() return map_unit.is_open_real_group_scene() end },
            { name = '商城', func = function() return cash_unit.get_cash_store_scene() end },
            { name = '队伍管理窗口', func = function() return party_unit.get_party_mgr_widger() ~= 0 end },
            { name = '队伍邀请接受窗口', func = function() return party_unit.get_invite_accept_window() ~= 0 end },
            { name = '制造委托', func = function() return make_unit.get_contract_craft_widget() ~= 0 end },
            { name = '提交的委托', func = function() return make_unit.get_contract_craft_cur_widget() ~= 0 end },
            { name = '铸币窗口', func = function() return api.get_wemix_widget() ~= 0 end },
            { name = '铸币二维码网页窗口', func = function() return api.get_wemix_web_widget() ~= 0 end },
            { name = '铸币计算器弹窗', func = function() return api.get_wemix_calculator_pop() ~= 0 end },
        }
    end
    for _, v in ipairs(this.spOpenWindowCfg) do
        if v.name == name then
            if v.func() then
                return 1
            else
                return -1
            end
        end
    end
    return 0
end

------------------------------------------------------------------------------------
-- 获取显示的UI
------------------------------------------------------------------------------------
function ui.get_child_id_by_control_and_tab_idx(ui_path, idx)
    -- 取tabBtn序号（从左往右0开始 就像交易行哪种   BTN先用子控件列表取出）
    -- ui_unit.get_tab_item_btn_idx(item_btn)
    local list = ui_unit.get_child_control_list(ui_path)
    for i = 1, #list do
        local child_control = list[i]
        if child_control ~= 0 and ui_unit.btn_is_visible(child_control) and ui_unit.get_tab_item_btn_idx(child_control) == idx then
            return child_control
        end
    end
    return 0
end

------------------------------------------------------------------------------------
-- 获取指定路径的UI
function ui.get_child_control_by_name_res(name)
    local path_info = ui_cfg.UI_INFO[name]
    if not table.is_empty(path_info) then
        return ui.get_child_control_visible(path_info.ui_path)
    end
    return 0
end

------------------------------------------------------------------------------------
-- 获取显示的UI
------------------------------------------------------------------------------------
function ui.get_child_control_visible(control)
    local list = ui_unit.get_child_control_list(control)
    for i = 1, #list do
        local child_control = list[i]
        if child_control ~= 0 and ui_unit.btn_is_visible(child_control) then
            return child_control
        end
    end
    return 0
end

------------------------------------------------------------------------------------
-- 点击指定名UI
---@param ui_name string ui_res配置的键
------------------------------------------------------------------------------------
function ui.click_ui_by_name(ui_name)
    local path_info = ui_cfg.UI_INFO[ui_name]
    if not table.is_empty(path_info) or type(ui_name) == 'number' then
        local child_control_id = 0
        if path_info.tab_idx then
            child_control_id = ui.get_child_id_by_control_and_tab_idx(path_info.ui_path, path_info.tab_idx)
        else
            child_control_id = type(ui_name) ~= 'number' and ui.get_child_control_visible(path_info.ui_path) or
                ui_name
        end
        if child_control_id ~= 0 and not ui_unit.btn_is_disabled(child_control_id) then
            trace.output("点击" .. ui_name)
            trace.log_trace("点击" .. ui_name)
            return ui_unit.btn_click(child_control_id)
        end
    end
    return false
end

--------------------------------------------------------------------------------
-- [读取] 根据名字关闭对应ui
--------------------------------------------------------------------------------
function ui.close_ui_by_creature_name(name)
    if name == "坐骑" then
        if ui.check_ui_info("坐骑页面") then
            api.exit_ui_widget()
        end
        return true
    elseif name == "滑翔翼" then
        if ui.check_ui_info("滑翔翼页面") then
            api.exit_ui_widget()
        end
        return true
    elseif name == "武器外形" then
        if ui.check_ui_info("武器外形页面") then
            api.exit_ui_widget()
        end
        return true
    end
    return false
end

return this
